package View;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

import Model.FileLoader;
import Model.MonteCarlo;
import Model.MonteCarloSingle;


public class Display {
	JButton launch=new JButton("launch 1 multi");
	JButton launch2=new JButton("launch 10 single");
	JButton launch3=new JButton("launch 10 multi");
	JButton launch4=new JButton("launch 10 multi with pools");
	JFrame frame=new JFrame("Repertoire");
	FileLoader loader=new FileLoader();
	JComboBox Listentries=new JComboBox(loader.loadTitles());
	JLabel result=new JLabel("Result:");
	JLabel execution=new JLabel("Execution time:");
	JLabel info=new JLabel("Please select parameters for the montecarlo function:");
	JProgressBar progress=new JProgressBar();
	
	String[]arg=new String[8];
	MonteCarlo calc;
	MonteCarloSingle calc2;
	Double value;
	Display that=this;
	long start,stop;
	
	public Display(){
		frame.add(info);
		frame.add(Listentries);
		frame.add(launch);
		frame.add(launch2);
		frame.add(launch3);
		frame.add(launch4);
		frame.add(result);
		frame.add(execution);
		frame.add(progress);

		
		launch.addActionListener(
				new ActionListener(){
					public void actionPerformed(
							ActionEvent e){
						start=System.currentTimeMillis();
						result.setText("result");
						calc=new MonteCarlo("c",Double.parseDouble(arg[0]),
								Double.parseDouble(arg[1]),
								Double.parseDouble(arg[2]),
								Double.parseDouble(arg[3]),
								Double.parseDouble(arg[4]),
								Double.parseDouble(arg[5]),
								Integer.parseInt(arg[6]),
								Integer.parseInt(arg[7]),that,0);
						try {
							calc.calculate();
							JLabel newLabel=new JLabel();
							//newLabel.setText(calc.getMonteCarloStandardOption()+" "+Long.toString(calc.getEnd() - calc.getStart()) + " millis");
							newLabel.setText("Poolsize: "+calc.getPoolSize()+calc.getMonteCarloStandardOption()+" "+Long.toString(System.currentTimeMillis()-stop) + " millis");
							frame.add(newLabel);
						} catch (ExecutionException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						progress.setMaximum(Integer.parseInt(arg[7]));
						progress.setMinimum(0);
						progress.setStringPainted(true);
						
					}
				}
				
				);
		launch2.addActionListener(
				new ActionListener() {
					public void actionPerformed(
							ActionEvent e) {
						start=System.currentTimeMillis();
						for(int i=0;i<10;i++){
						arg = Listentries.getItemAt(i).toString().split(",");
						calc2 = new MonteCarloSingle("c", Double.parseDouble(arg[0]),
								Double.parseDouble(arg[1]),
								Double.parseDouble(arg[2]),
								Double.parseDouble(arg[3]),
								Double.parseDouble(arg[4]),
								Double.parseDouble(arg[5]),
								Integer.parseInt(arg[6]),
								Integer.parseInt(arg[7]), that);
						
						progress.setMaximum(Integer.parseInt(arg[7]));
						progress.setMinimum(0);
						progress.setStringPainted(true);
						calc2.calculate();
						JLabel newLabel=new JLabel();
						//newLabel.setText(calc2.getMonteCarloStandardOption()+" "+Long.toString(calc2.getEnd() - calc2.getStart()) + " millis");
						newLabel.setText(calc2.getMonteCarloStandardOption()+" "+Long.toString(System.currentTimeMillis()-start) + " millis");
						frame.add(newLabel);
						}
						

					}
				});

		launch3.addActionListener(
				new ActionListener() {
					public void actionPerformed(
							ActionEvent e) {
						start=System.currentTimeMillis();
						for(int i=0;i<10;i++){
						arg = Listentries.getItemAt(i).toString().split(",");
						calc = new MonteCarlo("c", Double.parseDouble(arg[0]),
								Double.parseDouble(arg[1]),
								Double.parseDouble(arg[2]),
								Double.parseDouble(arg[3]),
								Double.parseDouble(arg[4]),
								Double.parseDouble(arg[5]),
								Integer.parseInt(arg[6]),
								Integer.parseInt(arg[7]), that,0);
						try {
							calc.calculate();
							JLabel newLabel=new JLabel();
							//newLabel.setText(calc.getMonteCarloStandardOption()+" "+Long.toString(calc.getEnd() - calc.getStart()) + " millis");
							newLabel.setText("Poolsize: "+calc.getPoolSize()+" "+calc.getMonteCarloStandardOption()+" "+Long.toString(System.currentTimeMillis()-start) + " millis");
							frame.add(newLabel);
						} catch (ExecutionException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						progress.setMaximum(Integer.parseInt(arg[7]));
						progress.setMinimum(0);
						progress.setStringPainted(true);
						}
						

					}
				});
		
		launch4.addActionListener(
				new ActionListener() {
					public void actionPerformed(
							ActionEvent e) {
						
						for(int i=0;i<10;i++){
							start=System.currentTimeMillis();
						arg = Listentries.getItemAt(0).toString().split(",");
						calc = new MonteCarlo("c", Double.parseDouble(arg[0]),
								Double.parseDouble(arg[1]),
								Double.parseDouble(arg[2]),
								Double.parseDouble(arg[3]),
								Double.parseDouble(arg[4]),
								Double.parseDouble(arg[5]),
								Integer.parseInt(arg[6]),
								Integer.parseInt(arg[7]), that,10+i*10);
						try {
							calc.calculate();
							JLabel newLabel=new JLabel();
							//newLabel.setText(calc.getMonteCarloStandardOption()+" "+Long.toString(calc.getEnd() - calc.getStart()) + " millis");
							newLabel.setText("Poolsize: "+calc.getPoolSize()+" "+calc.getMonteCarloStandardOption()+" "+Long.toString(System.currentTimeMillis()-start) + " millis");
							frame.add(newLabel);
						} catch (ExecutionException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						progress.setMaximum(Integer.parseInt(arg[7]));
						progress.setMinimum(0);
						progress.setStringPainted(true);
						}
						

					}
				});


		Listentries.addActionListener(
				new ActionListener(){
					public void actionPerformed(
							ActionEvent e){
						if(e.getSource()==Listentries){
							arg=Listentries.getSelectedItem().toString().split(",");
						}
						}
					}
				
				);

	
		frame.setLayout(new FlowLayout());
		frame.setResizable(false);
		frame.setSize(600,400);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	public void setResult(int simulationNumber){
		result.setText("result: "+Double.toString(calc.getMonteCarloStandardOption())+"("+simulationNumber+")");
	}
	
	public void setExecution(){
		execution.setText(Long.toString(calc.getEnd()-calc.getStart())+" millis");
	}
	
	public void setResultSingle(int simulationNumber){
		result.setText("result: "+Double.toString(calc2.getMonteCarloStandardOption())+"("+simulationNumber+")");
	}
	
	public void setExecutionSingle(){
		execution.setText(Long.toString(calc2.getEnd()-calc2.getStart())+" millis");
	}
	
	public void setBar(int step){
		progress.setValue(step);
	}

	public void addLabel(JLabel newLabel) {
		frame.add(newLabel);
		
	}
	

}




